<?php if (!defined('BASEPATH')) exit ('No direct script access allowed');

class Home extends CI_Controller {
	function __construct(){
		parent::__construct();
		$this->load->helper('url');
		$this->load->library('encrypt');
		$this->load->model('user_model');
		$this->load->model('lookup_model');
		$this->load->model('level_model');
	}
	
//	public function index(){
//		echo 'test';
//		$this->view('home');
//	}

//	public function index(){
//		$data['title'] = "雅思听力";
//		$this->load->view('templates/header', $data);
//		$this->load->view('question/list', $data);
//		$this->load->view('templates/footer');
//	}

	public function regist(){
		if (isset($_POST['email']) && isset($_POST['password']) && isset($_POST['con_password'])){
			$this->load->library('form_validation');
			$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
			$this->form_validation->set_rules('password', 'Password', 'trim|required');
			$this->form_validation->set_rules('con_password', 'Password', 'trim|required');
			if($this->form_validation->run() == TRUE){
				$data['title'] = "注册";
				if($_POST['password'] != $_POST['con_password']){
					$data['errorMsg'] = "密码不一致，注册失败！";
				}
				$user = $this->user_model->get_user_byemail($_POST['email']);
				if(empty($user)){
					$user['email'] = $_POST['email'];
					$arr = explode("@", $_POST['email']);
					$user['username'] = $arr[0];
					$user['userpwd'] = $_POST['password'];
					$this->user_model->user_insert($user);
					$user = $this->user_model->get_user_byemail($_POST['email']);
					$this->session->set_userdata('user', $user);
				}else{
					$data['errorMsg'] = "用户邮箱已使用，注册失败！";
				}
			}
		}
		
		if(isset($this->session->userdata['user'])){
			redirect('/');
		}else{
			$this->load->view('templates/header', $data);
			$this->load->view('pages/regist', $data);
			$this->load->view('templates/footer');
		}
	}
	
	public function login(){
		$data = array();
		if (isset($_POST['email']) && isset($_POST['password'])){
			$this->load->library('form_validation');
			$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
			$this->form_validation->set_rules('password', 'Password', 'trim|required');
			if($this->form_validation->run() == TRUE){
				$user = $this->user_model->login($_POST['email'], $_POST['password']);
				if($user){
					if($user['username'] == 'Admin'){
						$this->session->set_userdata('adminflag', true);
					}
					$this->session->set_userdata('user', $user);
				}else{
					$data['title'] = "登录";
					$data['errorMsg'] = "用户邮箱或者密码错误！";
				}
			}
		}
		if(isset($this->session->userdata['user'])){
			redirect('/');
		}else{
			$this->load->view('templates/header', $data);
			$this->load->view('pages/login', $data);
			$this->load->view('templates/footer');
		}
	}
	
	
//	public function view($page = 'home') {
//		if (file_exists('application/views/pages/'.$page.'.php')){
//	    	// 页面不存在
//	    	show_404();
//	  	}
//	  	echo 'test';
//	  	echo $page;
//	  
//	  	$data['title'] = ucfirst($page); // 将title中的第一个字符大写
//	  	if($page == 'home'){
//	  		home($data);
//	  	}
//	  	
//	  	$this->load->view('templates/header', $data);
//	  	$this->load->view('pages/'.$page, $data);
//	  	$this->load->view('templates/footer', $data);
//	}
	public function home($data){
		$levels = $this->level_model->list_level(array());
		$data['levels'] = $levels;
		return $data;
	}
	
}
